<template>
    <div>
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="query">
          <el-form-item>
            <el-input
              v-model="query.id"
              placeholder="部门编号"
              :clearable="true"
            ></el-input>
          </el-form-item>


          <el-form-item>
            <el-input
              v-model="query.deptName"
              placeholder="部门名称"
              :clearable="true"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" circle @click="showAll"></el-button>
            <el-button icon="el-icon-plus" circle @click="dialog.addshow = true"></el-button>
          </el-form-item>
        </el-form>
  
        <!-- addForm -->
        <el-dialog title="添加部门" :visible.sync="dialog.addshow" width="40%">
          <el-form :model="form">
            <el-form-item label="部门名称：" :label-width="formLabelWidth">
              <el-input v-model="addForm.deptName" autocomplete="off"></el-input>
            </el-form-item>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialog.addshow = false">取 消</el-button>
            <el-button type="primary" @click="addUser"
              >确 定</el-button
            >
          </div>
        </el-dialog>
  
  
        <!-- 用户修改 -->
        <el-dialog title="修改部门" :visible.sync="dialog.updateshow" width="40%">
          <el-form :model="form">
            <el-form-item label="部门名称：" :label-width="formLabelWidth">
              <el-input v-model="updateForm.deptName" autocomplete="off"></el-input>
            </el-form-item>
  
  
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialog.updateshow = false">取 消</el-button>
            <el-button type="primary" @click="updateUser"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column fixed prop="id" label="部门编号" width="120">
        </el-table-column>
        <el-table-column prop="deptName" label="部门名称" width="120">
        </el-table-column>
        
        <el-table-column fixed="right" label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="handleClick(scope.row.id)"
            ></el-button>
              <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  @click="handleDelete(scope.row.id)"
              ></el-button>
              
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.currentPage"
        :page-sizes="page.pageSizes"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </div>
  </template>
    
    <script>
  export default {
    methods: {
      handleSizeChange(val) {
        this.query.pageSize = val;
        this.showAll();
      },
      handleCurrentChange(val) {
        this.query.currentPage = val;
        this.showAll();
      },
      handleClick(row) {
          this.dialog.updateshow = true
          this.$axios.get("/depts/"+row)
          .then(response=>{
              this.updateForm = response.data.data.dept;
          })
      },
      handleDelete(row){
          this.$axios.delete("/depts/"+row)
          .then(response=>{
              if(response.data.code == 200){
                  this.$message.success("删除成功");    
              } else {
                this.$message.error("删除失败");
              }
              
          });
          this.showAll();
      },
      updateUser(){
          this.dialog.updateshow = false;
          this.$axios.put("/depts",this.updateForm)
          .then(response=>{
              if(response.data.code == 200){
                  this.$message.success("修改成功");  
                  this.showAll();  
              }
              
          });
      },
      showAll() {
        this.$axios.post("/depts/selAll", this.query).then((response) => {
          this.tableData = response.data.data.list;
          this.page.total = response.data.data.total;
        });
      },
      addUser() {
          this.dialog.addshow = false
          this.$axios.post("/depts/", this.addForm).then((response) => {
          if(response.data.code == 200){
              this.$massage.success("添加成功");
          }else {
              this.$massage.error("添加失败");
          }
        });
      },
    },
    mounted() {
      this.showAll();
    },
  
    data() {
      return {
        tableData: [],
        deptList: [],
        page: {
          pageSizes: [5, 10, 20, 50, 100], //下拉列表中的数组
          total: 0, //一共有多少条数据
        },
        query: {
          currentPage: 1, //代表当前页
          pageSize: 5, //代表每页显示多少条数据
          realName: "",
          dateRange: [],
        },
        dialog: {
          addshow: false,
          updateshow:false
        },
        addForm: {},
        updateForm: {},
      };
    },
  };
  </script>